package com.example.zzl.viewmodel.HomeFragment;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.example.zzl.R;
import com.example.zzl.adapter.CarouselPagerAdapter;
import com.example.zzl.adapter.HomeFragment.RecommendAdapter;
import com.example.zzl.adapter.RecyclerViewBaseAdapter;
import com.example.zzl.custom.MyTag;
import com.example.zzl.databinding.FragmentRecommentBinding;
import com.example.zzl.databinding.HomeHeadBinding;
import com.example.zzl.model.HomeFragment.RecommendModel;
import com.example.zzl.model.bean.Home;
import com.example.zzl.utils.specification.VolleySingleton;
import com.example.zzl.view.activity.HomeFragment.HotSpecialActivity;
import com.example.zzl.view.activity.HomeFragment.InfoActivity;
import com.example.zzl.view.activity.HomeFragment.InfoListActivity;
import com.example.zzl.view.activity.SpecialInfoActivity;
import com.example.zzl.viewmodel.BaseCallback;
import com.example.zzl.viewmodel.BaseOnClick;
import com.example.zzl.viewmodel.HomeFragment.implement.IRecommendVm;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ligg on 2017/2/9.
 */

public class RecommendPresenter implements IRecommendVm, SwipeRefreshLayout.OnRefreshListener, RadioGroup.OnCheckedChangeListener, RecyclerViewBaseAdapter.OnItemClickLitener, BaseOnClick{

    private final RecommendModel model;
    private Context context;
    private Callback callback;
    private FragmentRecommentBinding db;
    private List<Object> list;
    private RecommendAdapter adapter;
    private HomeHeadBinding headBinding;
    private CarouselPagerAdapter carouselAdapter;
    private List<Home.HeadData.ResultBean> result;
    private Home.IconData iconData;


    public void init() {
        list = new ArrayList<Object>();
        adapter = new RecommendAdapter(context, list, this);

        headBinding = DataBindingUtil.bind(View.inflate(context, R.layout.home_head, null));
        headBinding.setCallback(this);
        result = new ArrayList<>();
        carouselAdapter = new CarouselPagerAdapter(context, result);
        headBinding.vp.setAdapter(carouselAdapter);
    }

    public void initView() {
        db.rv.setLayoutManager(new LinearLayoutManager(context));
        db.rv.addHeaderView(getHeaderView());
        db.rv.setAdapter(adapter);
        db.swipeRl.setOnRefreshListener(this);
    }

    public void loadData() {
        model.loadData();
    }

    @Override
    public void loadFinished() {
        result.clear();
        Home.HeadData headData = model.getHeadData();
        result.addAll(headData.result);
        carouselAdapter.notifyDataSetChanged();
        headBinding.vp.setCurrentItem(200, false);

        iconData = model.getIconData();
        headBinding.setData(iconData);
        setIconDataIv();

        list.clear();
        list.addAll(model.getList());
        adapter.notifyDataSetChanged();
    }

    private void setIconDataIv() {
        for (int i = 0; i < 4; i++) {
            Home.IconData.ResultBean resultBean = iconData.result.get(i);
            final RadioButton rb;
            switch (i) {
                case 0:
                    rb = headBinding.checkIn;
                    break;

                case 1:
                    rb = headBinding.quanList;
                    break;

                case 2:
                    rb = headBinding.infoList;
                    break;

                case 3:
                    rb = headBinding.orderManager;
                    break;

                default:
                    rb = null;
                    break;
            }
            rb.setTag(resultBean.IcoImage);
            VolleySingleton.volleySingleton.getImageLoader().get(resultBean.IcoImage, new ImageLoader.ImageListener() {
                @Override
                public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                    if (rb.getTag().equals(response.getRequestUrl())) {
                        BitmapDrawable top = new BitmapDrawable(context.getResources(), response.getBitmap());
                        rb.setCompoundDrawablesWithIntrinsicBounds(null, top, null, null);
                    }
                }

                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("RecommendPresenter", "onErrorResponse: " + error.getMessage());
                }
            });
        }




    }

    @Override
    public void onRefresh() {
        db.swipeRl.setRefreshing(false);
    }

    public View getHeaderView() {
        View root = headBinding.getRoot();
        if (root.getParent() != null) {
            ((ViewGroup) root.getParent()).removeView(root);
        }
        return root;
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        Log.e("RecommendPresenter", "onCheckedChanged: " + checkedId);
        switch (checkedId) {
            case R.id.check_in:

                break;

            case R.id.quan_list:

                break;

            case R.id.info_list:
                context.startActivity(new Intent(context, InfoListActivity.class).putExtra("headTitle", "头条/咨询"));
                break;

            case R.id.order_manager:

                break;

            default:
                break;
        }
    }

    @Override
    public void onItemClick(View view, int position) {
        Log.e("RecommendPresenter", "tag :" + view.getTag() + "onItemClick: " + position);
        Bundle bundle;
        switch (view.getTag().toString()) {
            case "vp":
                bundle = new Bundle();
                bundle.putString("parameter", Integer.toString(((Home.OrderCommodity) list.get(0)).result.get(position).iInfoID));
                callback.startActivity(InfoActivity.class, bundle);
                break;

            case "iv":
                bundle = new Bundle();
                Integer itemPosition = (Integer) ((RecyclerView) view.getParent()).getTag();
                bundle.putString("parameter", Integer.toString(((Home.BrandExclusive) list.get(2)).result.get(itemPosition).listRecommendInfo.get(position).iInfoID));
                callback.startActivity(InfoActivity.class, bundle);
                break;

            default:
                String headTitle = ((Home.Special) list.get(1)).result.get(position).strParentSpecialName;
                bundle = new Bundle();
                bundle.putString("headTitle", headTitle);
                bundle.putInt("position", position);
                callback.startActivity(HotSpecialActivity.class, bundle);
                break;
        }
    }

    @Override
    public boolean onItemLongClick(View view, int position) {
        return false;
    }

    @Override
    public void myClick(View view) {
        switch (view.getId()) {
            case R.id.check_in:

                break;

            case R.id.quan_list:

                break;

            case R.id.info_list:
                context.startActivity(new Intent(context, InfoListActivity.class).putExtra("headTitle", "头条/咨询"));
                break;

            case R.id.order_manager:

                break;
            case R.id.iv:
                Home.BrandExclusive.ResultBean resultBean = ((Home.BrandExclusive) list.get(2)).result.get(((MyTag) view.getTag()).index);
                Bundle bundle = new Bundle();
                bundle.putString("headTitle", resultBean.strBrandExclusiveTitle);
                bundle.putInt("iSpecialID", resultBean.iBrandExclusiveID);
                callback.startActivity(SpecialInfoActivity.class, bundle);
                break;
            default:

                break;
        }

    }


    public interface Callback extends BaseCallback {
        void startActivity(Class cls, Bundle bundle);
    }

    public RecommendPresenter(Context context, Callback callback) {
        this.context = context;
        this.callback = callback;
        model = new RecommendModel(this);
    }

    public FragmentRecommentBinding getDb() {
        return db;
    }

    public void setDb(FragmentRecommentBinding db) {
        this.db = db;
    }



}
